(function(a) {

	var Loader = function(asset, option) {
		this.asset = {
			imgList: [],
			cssList: [],
			jsList: [],
			audioList: []
		};
		for (var key in this.asset) {
			this.asset[key] = asset[key] || [];
		}
		this.loaded = 0;
		this.total = this.asset.imgList.length + this.asset.cssList.length + this.asset.jsList.length + this.asset.audioList.length;
		this.prefix = option.prefix || '';
		this.debug = option && option.debug || false;
		this.load(option && option.progress);
	};

	Loader.prototype.load = function(cb) {
		if (this.debug) {
			var str = [
				'资源总数:' + this.total,
				'img:' + this.asset.imgList.length,
				'css:' + this.asset.cssList.length,
				'js:' + this.asset.jsList.length,
				'audio:' + this.asset.audioList.length
				].join('\n');
			console.log(str);
		}
		if (this.total === 0) return false;
		for (var key in this.asset) {
			if (this.asset[key].length > 0) {
				this.loadAsset(this.asset[key], cb);
			}
		}
	};

	Loader.prototype.loadAsset = function(list, cb) {
		var self = this;
		var total = list.length;
		for (var i = 0; i < total; i++) {
			var ext = list[i].substring(list[i].lastIndexOf('.')).toLowerCase();
			console.log(ext);
			if (ext === '.jpg' || ext === '.jpeg' || ext === '.png' || ext === '.gif') {
				var img = new Image();
				img.onload = function(e) {
					self.loaded++;
					cb && cb(self.loaded, self.total);
				};
				img.onerror = function(e) {
					self.loaded++;
					cb && cb(self.loaded, self.total);
					self.debug && console.log('img error: ' + self.prefix + list[i]);
				};
				img.src = this.prefix + list[i];
			} else if (ext === '.css') {
				var link = document.createElement('link');
				link.type = 'text/css';
				link.rel = 'stylesheet';
				link.href = this.prefix + list[i];
				link.onload = function(e) {
					self.total++;
					cb && cb(self.loaded, self.total);
				};
				link.onerror = function() {
					self.loaded++;
					cb && cb(self.loaded, self.total);
					self.debug && console.log('css error: ' + self.prefix + list[i]);
				};
				document.head.appendChild(link);
			} else if (ext === '.js') {
				var script = document.createElement('script');
				script.type = 'text/javascript';
				script.src = this.prefix + list[i];
				script.onload = function(e) {
					self.total++;
					cb && cb(self.loaded, self.total);
				};
				script.onerror = function() {
					self.loaded++;
					cb && cb(self.loaded, self.total);
					self.debug && console.log('script error: ' + self.prefix + list[i]);
				};
				document.head.appendChild(script);
			} else if (ext === '.mp3') {
				var audio = new Audio();
				audio.onload = function(e) {
					self.loaded++;
					cb && cb(self.loaded, self.total);
				};
				audio.onerror = function(e) {
					self.loaded++;
					cb && cb(self.loaded, self.total);
					self.debug && console.log('audio error: ' + self.prefix + list[i]);
				};
				audio.src = this.prefix + list[i];
			}
		}
	};

	a.YwY_Loader = Loader;
})(window);